const seq = require("../db/seq");
const { QueryTypes, Op, where } = require("sequelize");
const moment = require("moment");
const axios = require("axios");
const User = require("../model/user.model");
const Role = require("../model/role.model");
const GiteeClientId =
  "32813b2da3a3165de8eca86b77fa07076f1f274cb758d60b80583b56e657dd12";
const ClientSecret =
  "ad5eb50dcabced3dd48384097a82b83d1e02efc98a5a9e33e34407c4d22b04e8";
const RedirectUrl = "http://localhost/bl-user/callback";

class UserService {
  addUser({ username, password }) {
    const res = seq.query(
      "insert into `bl-user` (username,password) values (?,?)",
      {
        replacements: [username, password],
        type: QueryTypes.INSERT,
      }
    );
  }

  // 查询用户信息。
  async getUserMessage({ username, id }) {
    console.log(id, "userID");
    if (id) {
      const res = await seq.query("select * from `bl-user` where id=?", {
        replacements: [id],
        type: QueryTypes.SELECT,
      });
      console.log(res, "service---");
      return res ? res : "";
    }
    if (username) {
      const res = await seq.query("select * from `bl-user` where username=?", {
        replacements: [username],
        type: QueryTypes.SELECT,
      });
      console.log(res, "---res");
      return res ? res : "";
    }
  }
  async getUserInfo({ username, id }) {
    console.log(id, "userID");
    if (id) {
      const res = await seq.query("select * from `bl-user` where id=?", {
        replacements: [id],
        type: QueryTypes.SELECT,
      });
      console.log(res, "service---");
      return res ? res : "";
    }
    if (username) {
      const res = await seq.query("select * from `bl-user` where username=?", {
        replacements: [username],
        type: QueryTypes.SELECT,
      });
      console.log(res, "---res");
      return res ? res : "";
    }
  }

  // 使用token查询用户信息

  async getUserAccountInfo(token) {
    const res = await seq.query("select * from `bl-user` where password=?", {
      replacements: [token],
      type: QueryTypes.SELECT,
    });
    console.log("toeninfo", res);
    return res;
  }

  async queryUserInfo({ id }) {
    if (id) {
      const res = await seq.query("select * from `bl_user_info` where id=?", {
        replacements: [id],
        type: QueryTypes.SELECT,
      });
      console.log(res, "service---");
      return res ? res : "";
    }
  }

  //  第三方登录
  async getGiteeUserInfo(token) {
    const result = await axios.get(
      `https://gitee.com/api/v5/user?access_token=${token}`
    );
    return result;
  }
  async getGiteeService(code) {
    let res = await axios.post(
      `https://gitee.com/oauth/token?grant_type=authorization_code&code=${code}&client_id=${GiteeClientId}&redirect_uri=${RedirectUrl}&client_secret=${ClientSecret}`
    );
    console.log(res.data.access_token, "token");
    return res.data.access_token;
  }

  async setUserInfo(data, type) {
    //type:1 普通账号  type :2 gitee登录
    const { id, name, avatar_url, login } = data;
    console.log("id", id);
    const res = await User.findOne({ where: { id } });
    console.log(res, "result");
    const time = moment().valueOf();
    if (!res) {
      await User.create({
        id,
        nickname: name,
        avatar: avatar_url,
        username: login,
        createTime: time,
        lastLoginTime: time,
        loginType: 2,
      });
      const res = await User.findOne({ where: { id } });
      console.log(res, "result2");
      return res;
    } else {
      await User.update(
        { lastLoginTime: time },
        {
          where: { id },
        }
      );
      const res = await User.findOne({ where: { id } });
      console.log(res, "result2");
      return res;
    }
  }

  // 获取user列表
  async queryUserListServie(requestObj) {
    let paramObj = {};
    if (requestObj.nickname) {
      paramObj.nickname = {
        [Op.like]: `%${requestObj.nickname}%`,
      };
    }
    if (requestObj.loginType) {
      paramObj.loginType = requestObj.loginType;
    }
    const res = await User.findAll({ where: paramObj });
    let data = res.map((item) => {
      return { ...item.dataValues, password: null };
    });
    return data;
  }

  async queryRoleListService(requestObj) {
    let paramObj = {};
    if (requestObj.name) {
      paramObj.name = {
        [Op.like]: `%${requestObj.name}%`,
      };
    }
    const res = await Role.findAll({ where: paramObj });
    return res;
  }

  async modifyUserByIdService(requestObj) {
    const { id,username,titleImg,avatar,nickname,roleId,status } = requestObj;
    let obj = {username,avatar,nickname,roleId,status};
    if(titleImg) {
      obj.avatar = titleImg;
    }
    const res = await User.update(obj, {
      where: {
        id,
      },
    });
    console.log(requestObj, "?????修改");
    return res;
  }

  async modifyPwdService(requestObj) {
    await User.update({password:requestObj.newPwd},{
      where:{
        id:requestObj.id,
      }
    })
  }
}

module.exports = new UserService();
